package com.ruoyi.web.wrapper;

import cn.hutool.core.util.ObjectUtil;
import com.ruoyi.common.wrapper.BaseControllerWrapper;

import java.math.BigDecimal;
import java.math.RoundingMode;
import java.util.List;
import java.util.Map;

public class ChannelCountInfoWrapper extends BaseControllerWrapper {

    public ChannelCountInfoWrapper(Map<String, Object> single) {
        super(single);
    }

    public ChannelCountInfoWrapper(List<Map<String, Object>> multi) {

        super(multi);
    }


    private void calculateRate(Map<String, Object> map, String key, BigDecimal numerator, BigDecimal denominator) {
        if (ObjectUtil.isAllNotEmpty(numerator, denominator) && denominator.compareTo(BigDecimal.ZERO) != 0) {

            BigDecimal rate = numerator.divide(denominator, 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));

            map.put(key, String.format("%.2f%%", rate));
        } else {
            map.put(key, "0%");
        }
    }


    private void calculateComplexRate(Map<String, Object> map, String key, BigDecimal number, BigDecimal number1, BigDecimal number2) {
        if (ObjectUtil.isAllNotEmpty(number, number1, number2) && number1.add(number2).compareTo(BigDecimal.ZERO) != 0) {

            BigDecimal rate = number.divide(number1.add(number2), 4, RoundingMode.HALF_UP).multiply(new BigDecimal(100));

            map.put(key, String.format("%.2f%%", rate));
        } else {
            map.put(key, "0%");
        }
    }


    @Override
    public void wrapTheMap(Map<String, Object> map) {
        try {
            if (map != null && !map.isEmpty()) {

                // 获取各项数据
                BigDecimal merchPushCount = getBigDecimalValue(map.get("merchPushCount"));
                BigDecimal regCount = getBigDecimalValue(map.get("regCount"));
                BigDecimal uvProdProfit = getBigDecimalValue(map.get("uvProdProfit"));
                BigDecimal uvCost = getBigDecimalValue(map.get("uvCost"));
                BigDecimal regCost = getBigDecimalValue(map.get("regCost"));
                BigDecimal uvCount = getBigDecimalValue(map.get("uvCount"));
                BigDecimal sourceCount = getBigDecimalValue(map.get("sourceCount"));
                BigDecimal merchPushAmount = getBigDecimalValue(map.get("merchPushAmount"));
                BigDecimal sourceFineCount = getBigDecimalValue(map.get("sourceFineCount"));
                BigDecimal approvePushNumber = getBigDecimalValue(map.get("approvePushNumber"));


                // 检查空值
                if (ObjectUtil.isAllNotEmpty(regCount, uvCount,sourceFineCount,approvePushNumber, uvProdProfit,merchPushAmount,sourceCount,merchPushCount,uvCost,regCost)) {
                    // 注册率
                    calculateRate(map, "zhucelv", regCount, uvCount);
                    // 优质率
                    calculateRate(map, "yzlv", sourceFineCount, sourceCount);

                    // 注册成本
                    calculateRate(map, "pushRate", merchPushCount, regCount);

                    // 资质认证率
                    calculateRate(map, "zizhilv", sourceCount, regCount);
                    // 授权率
                    calculateRate(map, "approvePushNumberRate", approvePushNumber, sourceCount);

                    //uvROI
                    calculateComplexRate(map, "uvRoi", uvProdProfit, uvCost,regCost);

                    //pushRoi
                    calculateComplexRate(map, "pushRoi", merchPushAmount, uvCost,regCost);

                } else {
                    map.put("zhucelv", "0%");
                    map.put("pushRate", "0%");
                    map.put("zizhilv", "0%");
                    map.put("uvRoi", "0%");
                    map.put("approvePushNumberRate", "0%");
                    map.put("pushRoi", "0%");
                    map.put("yzlv", "0%");
                }
                // 进件成本
                BigDecimal totalCost = uvCost.add(regCost);
                if (sourceCount.compareTo(BigDecimal.ZERO) != 0) {
                    BigDecimal jjcb = totalCost.divide(sourceCount, 2, RoundingMode.HALF_UP);
                    map.put("jjcb", jjcb);
                } else {
                    map.put("jjcb", BigDecimal.ZERO);
                }

            }
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }


    // 方法用于将对象转换为BigDecimal
    private BigDecimal getBigDecimalValue(Object obj) {
        if (obj instanceof BigDecimal) {
            return (BigDecimal) obj;
        } else if (obj instanceof Integer) {
            return new BigDecimal((Integer) obj);
        } else if (obj instanceof String) {
            try {
                return new BigDecimal((String) obj);
            } catch (NumberFormatException e) {
                // 如果转换失败，返回0
                return BigDecimal.ZERO;
            }
        } else {
            // 其他情况返回0
            return BigDecimal.ZERO;
        }
    }
}
